/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HolidaysManagerDefault.java
 *
 * Created on Sep 1, 2011, 1:39:31 AM
 */
package holidaysManager;

import app.Global;
import app.MyCellRenderer;
import app.MyTable;
import app.MyView;
import entities.Holiday;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import lib.multiSpanCellTable.AttributiveCellTableModel;

/**
 *
 * @author yoshi
 */
public class HolidaysManagerDefault extends javax.swing.JPanel implements MyView{

    MyTable table = new MyTable();
    HolidayManagerModel myModel;
    HolidayManagerController myController;
    protected JScrollPane tableScroll;
    /** Creates new form HolidaysManagerDefault */
    public HolidaysManagerDefault() {
        initComponents();
        
        this.myModel = new HolidayManagerModel();
        this.myController = new HolidayManagerController();
        myController.setMyView(this);
        
        initModel();
        initTable();
        
        tableScroll = new JScrollPane(table);
        tableScroll.setSize(860, 350);
        contentPanel.add(tableScroll);
        
        Global.getFrame().setTitle("Holiday Manager");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        contentPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setLayout(new java.awt.BorderLayout());

        contentPanel.setPreferredSize(new java.awt.Dimension(881, 350));

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 881, Short.MAX_VALUE)
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
        );

        add(contentPanel, java.awt.BorderLayout.PAGE_START);

        jPanel2.setPreferredSize(new java.awt.Dimension(881, 60));

        jButton2.setText("Next");

        jButton3.setText("Previous");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 616, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel5.setPreferredSize(new java.awt.Dimension(881, 60));

        jButton1.setText("Add Holiday");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(717, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(81, Short.MAX_VALUE))
        );

        add(jPanel5, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    new AddHolidayDialog(Global.getFrame(), true, this).setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables

    private void initModel() {
        this.getMyModel().initDefault();
    }
    
    private void initTable(){
        ArrayList<Holiday> holidays = this.getMyModel().getHolidays();
        Vector vData = new Vector();
        Vector vRow = null;
        Vector vHead = new Vector(4);
        vHead.add("Date");
        vHead.add("Name");
        vHead.add("Edit");
        vHead.add("Delete");
        for (Holiday holiday : holidays) {
            vRow = new Vector(4);
            vRow.add(Global.getDefaultDateFormat().format(holiday.getDate()));
            vRow.add(holiday.getName());
            vRow.add(new EditButtonModel(holiday));
            vRow.add(new DeleteButtonModel(holiday));
            vData.add(vRow);
        }
        AttributiveCellTableModel ml = new AttributiveCellTableModel(vData, vHead);
        table.setModel(ml);
        table.getColumnModel().getColumn(0).setCellRenderer(new MyCellRenderer(SwingConstants.CENTER));
        table.makeButtonColumn(2, new EditButtonEditor(new JCheckBox(), this), new EditButtonRenderer());
        table.makeButtonColumn(3, new DeleteButtonEditor(new JCheckBox(), this), new DeleteButtonRenderer());
        table.makeItuneColumn(0);
        table.makeItuneColumn(1);
        table.setCellSelectionEnabled(false);
        
        table.setColumnWidth(0, 100);
        table.setColumnWidth(2, 150);
        table.setColumnWidth(3, 150);
    }

    @Override
    public HolidayManagerModel getMyModel() {
        return myModel;
    }

    @Override
    public HolidayManagerController getMyController() {
        return myController;
    }

    @Override
    public void refresh() {
        this.getMyModel().clear();
        this.initModel();
        this.initTable();
        tableScroll.setViewportView(table);
    }
}
